<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<%@ taglib prefix="thx" uri="/thx-tags"%>
<form id="signature-form" name="ui-adminlte-form" data-toggle="validator"
	role="form" method="post" enctype="multipart/form-data"
	action="${pageContext.request.contextPath}/workflow/signature!save.action"
	forward-action="${pageContext.request.contextPath}/workflow/signature!list.action">
	<s:token></s:token>
	<input name="id" value="${entity.signatureId }" type="hidden"/>
	<div class="form-group">
		<label>签章名称</label> 
		<input type="text" name="entity.name"
			value="${entity.name}" class="form-control required"
			remote="${pageContext.request.contextPath}/workflow/signature!checkSignatureName.action?id=${entity.signatureId}"
			placeholder="输入签章名称" />
	</div>
	<div class="form-group">
		<label>用户名称 </label> 
		<input type="text" name="entity.username"
			value="${entity.username}" class="form-control required"
			placeholder="输入用户名称" />
	</div>
	<div class="form-group">
		<label>用户密码 </label> 
		<input type="text" name="entity.password"
			value="${entity.password}" class="form-control required"
			placeholder="输入用户密码" />
	</div>
	<div class="form-group">
		<label>签章文件</label>
		<!-- 修改签章 文件域不是必须的 -->
		<c:if test="${entity.signatureId != null}">
			<input type="file" name="image" />
		</c:if>
		<!--新增签章  -->
		<c:if test="${entity.signatureId == null}">
			<input type="file" name="image" class="required" />
		</c:if>	
	</div>
	<c:if test="${entity.signatureId != null}">
	<div class="form-group">
		<label></label>
		<img alt="" width="120" height="100" src="${pageContext.request.contextPath}/workflow/signature!showPicture.action?id=${entity.signatureId}">
	</div>
	</c:if>
</form>

<script>

	var signatureForm = {};
	
	signatureForm.validateConf = {
		errorElement : 'span',
		errorClass : 'help-block',
		errorPlacement : function(error, element) {
			element.next().remove();
			element.after('<span class="glyphicon glyphicon-remove form-control-feedback" aria-hidden="true"></span>');
			element.closest('.form-group').append(error);
		},
		highlight : function(element) {
			$(element).closest('.form-group').addClass('has-error has-feedback');
		},
		success : function(label) {
			var el = label.closest('.form-group').find("input");
			el.next().remove();
			el.after('<span class="glyphicon glyphicon-ok form-control-feedback" aria-hidden="true"></span>');
			label.closest('.form-group').removeClass('has-error').addClass("has-feedback has-success");
			label.remove();
		}
	};

	signatureForm.setValidatorMessage = function() {
		jQuery.extend(jQuery.validator.messages, {
			required : "必填",
			remote : "已存在",
			email : "请输入正确格式的电子邮件",
			url : "请输入合法的网址",
			date : "请输入合法的日期",
			dateISO : "请输入合法的日期 (ISO).",
			number : "请输入合法的数字",
			digits : "只能输入整数",
			creditcard : "请输入合法的信用卡号",
			equalTo : "请再次输入相同的值",
			accept : "请输入拥有合法后缀名的字符串",
			maxlength : jQuery.validator.format("请输入一个 长度最多是 {0} 的字符串"),
			minlength : jQuery.validator.format("请输入一个 长度最少是 {0} 的字符串"),
			rangelength : jQuery.validator.format("请输入 一个长度介于 {0} 和 {1} 之间的字符串"),
			range : jQuery.validator.format("请输入一个介于 {0} 和 {1} 之间的值"),
			max : jQuery.validator.format("请输入一个最大为{0} 的值"),
			min : jQuery.validator.format("请输入一个最小为{0} 的值")
		});
	};
	
	signatureForm.checkData = function() {
		return $("#signature-form").valid();
	};
	
	signatureForm.callBackFunc = function (msg) {
		$("#signature-form").parents('.modal').modal('hide');
		var forwardAction = $("#signature-form").attr('forward-action');
		var container = $("#signature-form").attr('container');
		if (msg.code == "success"){
			if(forwardAction){
				dialog.calloutAlert(msg.desc,"","success");
				// modify xjw 优化表单提交后页面跳转
				util.ajaxPage(forwardAction, '', false, container);
			}else
				dialog.alert(msg.desc);
		}else{
			if(forwardAction)
				dialog.calloutAlert(msg.desc,"","error");
			else
				dialog.alert(msg.desc);
		}
	};
	
	signatureForm.options = {
        beforeSubmit: function(){
        	if(signatureForm.checkData()){
        		return true;
        	}else{
        		return false;
        	}
        },  
        success: function(msg){
        	msg = util.strToObj(msg);
        	signatureForm.callBackFunc(msg);
        } 
    };
	
	signatureForm.init = function() {
		signatureForm.setValidatorMessage();
		$("#signature-form").validate(signatureForm.validateConf);
	
		// 解决相同name的input只教验第一个bug，要求：name 相同的 input id 必须不同
		if ($.validator) {
			// fix: when several input elements shares the same name, but
			// has different id-ies....
			$.validator.prototype.elements = function() {
				var validator = this , rulesCache = {};
				// select all valid inputs inside the form (no submit or
				// reset buttons)
				// workaround $Query([]).add until
				// http://dev.jquery.com/ticket/2114 is solved
				return $([]).add(this.currentForm.elements).filter(":input").not(":submit, :reset, :image, [disabled]")
						.not(this.settings.ignore).filter(
								function() {
									var elementIdentification = this.id || this.name;
									!elementIdentification && validator.settings.debug && window.console && console.error(
											"%o has no id nor name assigned", this);
									// select only the first element for each name,
									// and only
									// those with rules specified
									if (elementIdentification in rulesCache || !validator.objectLength($(this).rules()))
										return false;
									rulesCache[elementIdentification] = true;
									return true;
								});
			};
		}
		$("#signature-form").ajaxForm(signatureForm.options);
	};
	
	$(function(){
		signatureForm.init();
	});
</script>